<?php
/**
 * /library/mvc-helper/ActionHelper/PageParams.php
 * @author qqinxl
 * 2008/03/24
 * 
 * @category  
 * @package  
 * @copyright  Copyright (c) {}
 */
class ActionHelper_PageParams extends Zend_Controller_Action_Helper_Abstract {
	public function isNeedChangeEncode($autoChange,$_encode) {
		if (! empty ( $_encode ) and strtolower ( $_encode ) != 'utf-8' and $autoChange) {
			return true;
		}
		return false;
	}
	
	public function changeRedirectUrl($url){
		if(substr($url,0,strlen('/')) === '/'){
			$url = trim($url);
		}else{
			$url = '/'.trim($url);
		}
		$url = $url.'?'.SID;	
		return $url;
	}

	public function getParams($autoChange,$_encode){
		try{
			$_paras = $this->getRequest()->getParams();
			if($this->isNeedChangeEncode($autoChange,$_encode)){
				foreach($_paras as $key=>$value){
					if(!empty($value) and is_string($value)){
						$_paras[$key] = mb_convert_encoding($value,'UTF-8', $_encode);
					}
				}
			}			
		}catch (Exception $e){
			throw $e;
		}
		return $_paras;
	}
	
	public function getParam($key,$autoChange,$_encode){
		try{
			$_paras = $this->getRequest()->getParam($key);			
			if($this->isNeedChangeEncode($autoChange,$_encode)){
				if(!empty($_paras) and is_string($_paras)){
					$_paras = mb_convert_encoding($_paras,'UTF-8', $_encode);
				}
			}			
		}catch (Exception $e){
			throw $e;
		}
		return $_paras;
	}
	
	public function getPosts($autoChange,$_encode){
		try{
			if($this->getRequest()->isPost()){
				$values = $this->getRequest()->getPost();
				if(empty($values)){
					return null;
				}
				$filter = new Zend_Filter_StripTags();
				foreach($values as $key=>$value){
					if(!empty($value) and is_string($value )){
						$value = $filter->filter($value);
						$value= trim($value );
						if($this->isNeedChangeEncode($autoChange,$_encode)){
							$values[$key]  = mb_convert_encoding($value ,'UTF-8',$_encode);
						}else{
							$values[$key]  = $value;
						}
					}
				}
				return $values;
			}
		}catch (Exception $e){
			throw $e;
		}
		return null;
	}
	
	public function getPost($key,$autoChange,$_encode){
		try{
			if($this->getRequest()->isPost()){
				$filter = new Zend_Filter_StripTags();
				$value= $filter->filter($this->getRequest()->getPost($key));
				$value = trim($value);
				if(!empty($value) and is_string($value)){
					if($this->isNeedChangeEncode($autoChange,$_encode)){
						$value = mb_convert_encoding($value,'UTF-8', $_encode);
					}
				}
				return $value;
			}
		}catch (Exception $e){
			throw $e;
		}
		
		return null;
	}
}